<html>
<head><meta charset="utf-8"><title>Is this UB? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html">Is this UB?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="219352225"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219352225" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219352225">(Dec 09 2020 at 15:33)</a>:</h4>
<p>I sent a patch to refactor stuff which involves touching a bit of unsafe but mark simulacrum mentioned that it is unsafe. Why is it? <a href="https://github.com/rust-lang/rust/pull/79722/files">https://github.com/rust-lang/rust/pull/79722/files</a></p>



<a name="219352511"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219352511" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219352511">(Dec 09 2020 at 15:35)</a>:</h4>
<p>It is UB to read from a value that was moved out of</p>



<a name="219352969"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219352969" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219352969">(Dec 09 2020 at 15:38)</a>:</h4>
<p><span class="user-mention" data-user-id="211727">@Jonas Schievink</span> Can you point me to which part it is specifically?</p>



<a name="219353114"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219353114" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219353114">(Dec 09 2020 at 15:39)</a>:</h4>
<p><code>alloc_from_iter</code> reads the array elements, the expression <code>|| mem::forget(self)</code> moves <code>self</code> into the closure</p>



<a name="219353207"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219353207" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219353207">(Dec 09 2020 at 15:39)</a>:</h4>
<p>But that only gets called later right?</p>



<a name="219353222"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219353222" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219353222">(Dec 09 2020 at 15:39)</a>:</h4>
<p>In <code>clean()</code>.</p>



<a name="219353332"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219353332" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219353332">(Dec 09 2020 at 15:40)</a>:</h4>
<p>The closure expression itself moves out of <code>self</code></p>



<a name="219353350"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219353350" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219353350">(Dec 09 2020 at 15:40)</a>:</h4>
<p>Not the call</p>



<a name="219353377"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219353377" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219353377">(Dec 09 2020 at 15:40)</a>:</h4>
<p>Thus the point about the lack of <code>move</code> annotation.</p>



<a name="219353452"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219353452" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219353452">(Dec 09 2020 at 15:41)</a>:</h4>
<p>this means alloc_from_iter is unsound, right? there's no <code>unsafe</code> in the added code</p>



<a name="219353492"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219353492" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219353492">(Dec 09 2020 at 15:41)</a>:</h4>
<p>seems weird it takes a raw pointer without being marked unsafe</p>



<a name="219353512"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219353512" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219353512">(Dec 09 2020 at 15:41)</a>:</h4>
<p>Ah.</p>



<a name="219353545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219353545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219353545">(Dec 09 2020 at 15:41)</a>:</h4>
<p>So I need to add <code>unsafe</code> to the function or should I close the pull request?</p>



<a name="219353620"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219353620" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219353620">(Dec 09 2020 at 15:42)</a>:</h4>
<p><span class="user-mention" data-user-id="225422">@pickfire</span> to be clear, adding unsafe will not fix the UB</p>



<a name="219353633"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219353633" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219353633">(Dec 09 2020 at 15:42)</a>:</h4>
<p>That doesn't fix the UB</p>



<a name="219353643"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219353643" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219353643">(Dec 09 2020 at 15:42)</a>:</h4>
<p>it will just make it more clear how it's happening</p>



<a name="219353940"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219353940" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219353940">(Dec 09 2020 at 15:44)</a>:</h4>
<p>What should I do?</p>



<a name="219353956"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219353956" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219353956">(Dec 09 2020 at 15:44)</a>:</h4>
<p>Is there any way to fix it?</p>



<a name="219354017"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219354017" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219354017">(Dec 09 2020 at 15:44)</a>:</h4>
<p>well, there might be a different way to write alloc_from_iter, but I don't think this implementation can be fixed, no</p>



<a name="219354428"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219354428" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219354428">(Dec 09 2020 at 15:47)</a>:</h4>
<p>T_T</p>



<a name="219354450"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219354450" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219354450">(Dec 09 2020 at 15:47)</a>:</h4>
<p>Thanks, so I guess I should just close the PR.</p>



<a name="219354524"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219354524" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219354524">(Dec 09 2020 at 15:48)</a>:</h4>
<p>yeah, sorry :/</p>



<a name="219354525"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219354525" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219354525">(Dec 09 2020 at 15:48)</a>:</h4>
<p>I am still confused "the closure expression moves out of <code>self</code>"</p>



<a name="219354609"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219354609" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219354609">(Dec 09 2020 at 15:48)</a>:</h4>
<p><code>self</code> is a <code>[T; N]</code> by value</p>



<a name="219354701"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219354701" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219354701">(Dec 09 2020 at 15:49)</a>:</h4>
<p>when you pass it to the closure, it moves from the stack to the closure struct</p>



<a name="219354734"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219354734" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219354734">(Dec 09 2020 at 15:49)</a>:</h4>
<p>i.e. it's captured and the address in memory changes</p>



<a name="219354770"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219354770" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219354770">(Dec 09 2020 at 15:49)</a>:</h4>
<p>so .as_slice().as_ptr() is now dangling</p>



<a name="219355155"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219355155" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219355155">(Dec 09 2020 at 15:52)</a>:</h4>
<p>Ah, the memory address changed, I thought it is still the same.</p>



<a name="219355187"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219355187" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219355187">(Dec 09 2020 at 15:52)</a>:</h4>
<p>any move is allowed to change the address</p>



<a name="219355231"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219355231" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219355231">(Dec 09 2020 at 15:53)</a>:</h4>
<p>in <em>practice</em> it might not which is why the tests were passing, but it's still UB to depend on the address remaining fixed after a move</p>



<a name="219355246"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219355246" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219355246">(Dec 09 2020 at 15:53)</a>:</h4>
<p>More than anything - reading a value after it has moved is UB. It doesn't matter what actually happens - anything _can_ happen.</p>



<a name="219355376"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20UB%3F/near/219355376" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20UB.3F.html#219355376">(Dec 09 2020 at 15:54)</a>:</h4>
<p>The fact that it might work is actually worse. it's not guaranteed to continue working</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>